package cn.school.demo.ui.point_datails

import android.annotation.SuppressLint
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import cn.school.demo.MyApplication
import cn.school.demo.pojo.IntegralDetails
import cn.school.demo.repository.IntegralDetailsRepository
import com.blankj.utilcode.util.LogUtils
import java.text.FieldPosition
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

class PointDetailsViewModel : ViewModel() {
    private var _integralDetailsList: LiveData<MutableList<IntegralDetails>>
    val integralDetailsList: LiveData<MutableList<IntegralDetails>> get() = _integralDetailsList

    private val _classifyMapList = mutableMapOf<String, MutableList<IntegralDetails>>()
    val classifyMapList: Map<String, MutableList<IntegralDetails>> get() = _classifyMapList

    private val _classifyMapAllPointList = mutableMapOf<String, Int>()
    val classifyMapAllPointList: Map<String, Int> get() = _classifyMapAllPointList

    private val _thisItemRecycleViewList =
        MutableLiveData<MutableList<IntegralDetails>>(mutableListOf())
    val thisItemRecycleViewList: LiveData<MutableList<IntegralDetails>> get() = _thisItemRecycleViewList

    private val _observeData = MutableLiveData<MutableList<IntegralDetails>>(mutableListOf())
    val observeData: LiveData<MutableList<IntegralDetails>> get() = _observeData

    @SuppressLint("NewApi")
    private val _thisDateItem =
        MutableLiveData<IntegralDetails>(
            IntegralDetails(
                integralTime = LocalDateTime.now().toString()
            )
        )
    val thisDateItem: LiveData<IntegralDetails> get() = _thisDateItem

    private val _timeList = mutableListOf<IntegralDetails>()
    val timeList: MutableList<IntegralDetails> get() = _timeList

    private var _oldPosition: Int = 34
    val oldPosition: Int get() = _oldPosition
    private var _nowPosition: Int = 34
    val nowPosition: Int get() = _nowPosition

    init {
        _integralDetailsList =
            IntegralDetailsRepository().getAllIntegralDetailsById(MyApplication.user!!.sfNumber)
    }

    fun setOldPosition(position: Int) {
        _oldPosition = position
    }
    fun setNowPosition(position: Int) {
        _nowPosition = position
    }

    fun updateThisDateItem(item: IntegralDetails) {
        LogUtils.e(item)
        _thisDateItem.value = item
    }

    fun updateThisList(list: MutableList<IntegralDetails>) {
        _thisItemRecycleViewList.value!!.clear()
        _thisItemRecycleViewList.value!!.addAll(list)
        _observeData.value = list
        LogUtils.e(list.toString(), _thisItemRecycleViewList.value.toString())
    }

    fun classifyIntegralDetailsList() {
        LogUtils.e(_integralDetailsList.value!!.size)
        _integralDetailsList.value?.forEach {
            if (_classifyMapList.containsKey(
                    it.integralTime.substring(
                        0,
                        10
                    )
                ) && _classifyMapList[it.integralTime.substring(0, 10)] != null
            ) {
                _classifyMapList[it.integralTime.substring(0, 10)]!!.add(it)
                val point = _classifyMapAllPointList[it.integralTime.substring(
                    0,
                    10
                )]!! + it.integralAccount
                _classifyMapAllPointList[it.integralTime.substring(0, 10)] = point
            } else {
                val list = mutableListOf<IntegralDetails>()
                list.add(it)
                _classifyMapList[it.integralTime.substring(0, 10)] = list
                _classifyMapAllPointList[it.integralTime.substring(0, 10)] = it.integralAccount
            }
        }
        LogUtils.e(_classifyMapAllPointList.toString(), _classifyMapList.toString())
    }

    @SuppressLint("NewApi")
    fun setTimeList() {
        _timeList.clear()
        for (i in 0..2) {
            _timeList.add(IntegralDetails(integralTime = "", integralAccount = -1))
        }
        var nowDate = LocalDate.now().minusDays(31)
        LogUtils.e(nowDate)
        for (i in 0..31) {
            if (_classifyMapAllPointList[nowDate.toString()] != null
            ) {
                _timeList.add(
                    IntegralDetails(
                        userSfNumber = MyApplication.user!!.sfNumber,
                        integralAccount = _classifyMapAllPointList[nowDate.toString()]!!,
                        integralTime = nowDate.toString()
                    )
                )
            } else {
                _timeList.add(
                    IntegralDetails(
                        integralAccount = 0,
                        userSfNumber = MyApplication.user!!.sfNumber,
                        integralTime = nowDate.toString()
                    )
                )
            }
            nowDate = nowDate.plusDays(1)
        }
        for (i in 0..2) {
            _timeList.add(IntegralDetails(integralTime = "", integralAccount = -1))
        }
        LogUtils.e(_timeList.size)
    }
}